package com.palm.hubo2o.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;

/**
 * Created by dest1 on 2015/10/2.
 */
public abstract class BallGroupAdapter extends RecyclerView.Adapter<BallGroupAdapter.BallHolder> {
    private int[] groups;//球区数量数组
    private int offset;
    private String ballFormat;


    public enum ITEM_TYPE {ITEM_TYPE_CONTENT, ITEM_TYPE_MARK}

    public void setBallNumber(int... numbers) {
        this.groups = new int[numbers.length];
        for (int i = 0; i < groups.length; i++) {
            if (i == 0) {
                groups[0] = numbers[0] + 1;
            } else {
                groups[i] = groups[i - 1] + numbers[i] + 1;
            }
        }
    }

    @Override
    public BallHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BallHolder holder;
        if (viewType == ITEM_TYPE.ITEM_TYPE_MARK.ordinal()) {
            holder = onCreateLabelHolder(parent);
            holder.setType(ITEM_TYPE.ITEM_TYPE_MARK);
        } else {
            holder = onCreateItemHolder(parent);
            holder.setType(ITEM_TYPE.ITEM_TYPE_CONTENT);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(BallHolder holder, int position) {
        int index = getItemGroupIndex(position);
        if (ITEM_TYPE.ITEM_TYPE_MARK == holder.getType()) {
            onBindMarkViewHolder(holder, index);
        } else {
            onBindItemViewHolder(holder, index, index == 0 ? position - 1 : position - groups[index - 1] - 1);
        }
    }

    @Override
    public int getItemCount() {

        return groups[groups.length - 1];
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || Arrays.binarySearch(groups, position) >= 0) {
            return ITEM_TYPE.ITEM_TYPE_MARK.ordinal();

        } else {
            return ITEM_TYPE.ITEM_TYPE_CONTENT.ordinal();
        }
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getOffset() {
        return offset;
    }

    public String getBallFormat() {
        return ballFormat;
    }

    public void setBallFormat(String ballFormat) {
        this.ballFormat = ballFormat;
    }

    /**
     * 获取属于哪个球组的位置
     *
     * @param position
     * @return
     */
    public int getItemGroupIndex(int position) {
        for (int i = 0; i < groups.length; i++) {
            if (position < groups[i])
                return i;
        }
        return -1;
    }

    /**
     * 获取球中的位置
     *
     * @param position
     * @param groupIndex
     * @return
     */
    public int getPositionInGroup(int position, int groupIndex) {
        if (groupIndex > 0)
            position -= groups[groupIndex - 1];
        return position - 1;
    }

    /**
     * 转换成原始位置
     *
     * @return
     */
    public int transToRawPosition(int posInGroup, int groupIndex) {
        if (groupIndex > 0) {
            posInGroup += groups[groupIndex - 1];
        }
        return posInGroup + 1;
    }

    public abstract BallHolder onCreateItemHolder(ViewGroup parent);

    public abstract BallHolder onCreateLabelHolder(ViewGroup parent);

    public abstract void onBindItemViewHolder(BallHolder holder, int groupIndex, int position);

    public abstract void onBindMarkViewHolder(BallHolder holder, int groupIndex);

    public static class BallHolder extends RecyclerView.ViewHolder {
        private ITEM_TYPE getType() {
            return type;
        }

        private void setType(ITEM_TYPE type) {
            this.type = type;
        }

        public ITEM_TYPE type;

        public BallHolder(View view) {
            super(view);
        }
    }
}
